package com.yugao.fintech.antelope.mps.core.channel.client;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.yugao.fintech.antelope.base.model.module.config.param.sms.JuMeiSmsParam;
import com.yugao.fintech.antelope.mps.api.constatnts.ChannelCodeEnum;
import com.yugao.fintech.antelope.mps.api.model.content.MsgContent;
import com.yugao.fintech.antelope.mps.core.PushTaskContext;
import com.yugao.fintech.antelope.mps.core.channel.MsgChannel;
import com.yugao.fintech.antelope.mps.core.parser.AbstractTemplateParser;
import com.yugao.fintech.antelope.mps.core.parser.SmsTemplateParser;
import com.yugao.fintech.antelope.mps.model.rqrs.channel.SmsChannelResp;
import com.yugao.fintech.framework.core.utils.SpringUtils;
import com.yugao.fintech.framework.sms.manager.JMSmsManager;
import com.yugao.fintech.framework.sms.manager.entity.JMSmsReps;
import com.yugao.fintech.framework.sms.manager.entity.JMSmsReq;
import com.yugao.fintech.framework.sms.properties.JMSmsProperties;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * 聚美短信客户端

 */
@Getter
@Setter
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class SmsJmMsgClient extends AbstractSmsMsgClient {
    private JMSmsManager jmSmsManager;
    private JMSmsProperties jmSmsProperties;

    @Override
    public ChannelCodeEnum channelCode() {
        return ChannelCodeEnum.SMS_JUMEI;
    }

    @Override
    public void init() {
        RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class);
        JuMeiSmsParam paramConfig = (JuMeiSmsParam) msgChannel.getParamConfig();
        this.jmSmsProperties = new JMSmsProperties();
        this.jmSmsProperties.setAppCode(paramConfig.getAppCode());
        this.jmSmsProperties.setEnable(true);
        this.jmSmsManager = new JMSmsManager(jmSmsProperties, restTemplate);
    }

    public SmsJmMsgClient(MsgChannel msgChannel) {
        super(msgChannel);
    }

    @Override
    public SmsChannelResp send(PushTaskContext context) {
        MsgContent msgContent = (MsgContent) context.getMsgContent();
        AbstractTemplateParser templateParser = context.getTplConfig();
        SmsTemplateParser templateConfig = (SmsTemplateParser) context.getTplConfig();
        log.debug("send msg to sms, msg: {}", JSON.toJSONString(templateConfig, JSONWriter.Feature.WriteEnumsUsingName));

        JSONObject data = new JSONObject();
        templateParser.getVarData().values().forEach(e -> data.put(e.getKey(), e.getValue()));

        JMSmsReps reps = jmSmsManager.send(JMSmsReq.builder()
                .templateId(templateConfig.getTemplateId()).templateParam(data)
                .phoneNumbers(msgContent.getToUser()).build());
        boolean success = reps.isSuccess();
        return SmsChannelResp.builder().success(success).content(JSON.toJSONString(reps)).build();
    }
}
